﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace IssueTrackerTest
{
    /// <summary>
    /// Unit test class for ProjectBO
    /// </summary>
    [TestClass]
    public class ProjectTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestMethod]
        public void TestAddProject()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            IssueTrackerService.Project entity = new IssueTrackerService.Project();

            entity.Name = "Mock Test Project";
            entity.Description = "Mock Test Project";
            entity.CreatorId = "patrickyong";
            entity.ProjectManagerId = "patrickyong";

            try
            {
                client.Open();

                IssueTrackerService.Project result = client.CreateProject(entity);

                Assert.IsNotNull(result.Id);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);

            }
            finally
            {
                client.Close();
            }
        }

        [TestMethod]
        public void TestUpdateProject()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            List<IssueTrackerService.Project> list;

            try
            {
                client.Open();
                list = client.ListProjects().ToList();

                if (list != null)
                {
                    IssueTrackerService.Project entity = list.First();

                    entity.Description = "Mock Test Updated";
                    IssueTrackerService.Project result = client.UpdateProject(entity);

                    Assert.AreEqual("Mock Test Updated", result.Description);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
            
            
        }

        [TestMethod]
        public void TestDeleteProject()
        {
            IssueTrackerService.IssueTrackerServiceClient client
                = new IssueTrackerService.IssueTrackerServiceClient();

            int id;
            List<IssueTrackerService.Project> list;

            try
            {
                client.Open();

                list = client.ListProjects().ToList();

                if (list != null)
                {
                    IssueTrackerService.Project entity = list.First();

                    id = entity.Id;

                    client.DeleteProject(entity);

                    var checkProject = client.GetProject(id);

                    Assert.IsNull(checkProject);
                }
                else
                {
                    Assert.Fail("Project Table is empty");
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
            finally
            {
                client.Close();
            }
        }
    }
}
